home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / gcc.info-7 (.txt) < prev    next >
GNU Info File  |  1993-10-21  |  51KB  |  942 lines

  1. This is Info file gcc.info, produced by Makeinfo-1.54 from the input
  2. file gcc.texi.
  3.    This file documents the use and the internals of the GNU compiler.
  4.    Published by the Free Software Foundation 675 Massachusetts Avenue
  5. Cambridge, MA 02139 USA
  6.    Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided also
  12. that the sections entitled "GNU General Public License" and "Protect
  13. Your Freedom--Fight `Look And Feel'" are included exactly as in the
  14. original, and provided that the entire resulting derived work is
  15. distributed under the terms of a permission notice identical to this
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that the sections entitled "GNU General Public
  19. License" and "Protect Your Freedom--Fight `Look And Feel'", and this
  20. permission notice, may be included in translations approved by the Free
  21. Software Foundation instead of in the original English.
  22. File: gcc.info,  Node: Variable Length,  Next: Macro Varargs,  Prev: Zero Length,  Up: C Extensions
  23. Arrays of Variable Length
  24. =========================
  25.    Variable-length automatic arrays are allowed in GNU C.  These arrays
  26. are declared like any other automatic arrays, but with a length that is
  27. not a constant expression.  The storage is allocated at the point of
  28. declaration and deallocated when the brace-level is exited.  For
  29. example:
  30.      FILE *
  31.      concat_fopen (char *s1, char *s2, char *mode)
  32.      {
  33.        char str[strlen (s1) + strlen (s2) + 1];
  34.        strcpy (str, s1);
  35.        strcat (str, s2);
  36.        return fopen (str, mode);
  37.      }
  38.    Jumping or breaking out of the scope of the array name deallocates
  39. the storage.  Jumping into the scope is not allowed; you get an error
  40. message for it.
  41.    You can use the function `alloca' to get an effect much like
  42. variable-length arrays.  The function `alloca' is available in many
  43. other C implementations (but not in all).  On the other hand,
  44. variable-length arrays are more elegant.
  45.    There are other differences between these two methods.  Space
  46. allocated with `alloca' exists until the containing *function* returns.
  47. The space for a variable-length array is deallocated as soon as the
  48. array name's scope ends.  (If you use both variable-length arrays and
  49. `alloca' in the same function, deallocation of a variable-length array
  50. will also deallocate anything more recently allocated with `alloca'.)
  51.    You can also use variable-length arrays as arguments to functions:
  52.      struct entry
  53.      tester (int len, char data[len][len])
  54.      {
  55.        ...
  56.      }
  57.    The length of an array is computed once when the storage is allocated
  58. and is remembered for the scope of the array in case you access it with
  59. `sizeof'.
  60.    If you want to pass the array first and the length afterward, you can
  61. use a forward declaration in the parameter list--another GNU extension.
  62.      struct entry
  63.      tester (int len; char data[len][len], int len)
  64.      {
  65.        ...
  66.      }
  67.    The `int len' before the semicolon is a "parameter forward
  68. declaration", and it serves the purpose of making the name `len' known
  69. when the declaration of `data' is parsed.
  70.    You can write any number of such parameter forward declarations in
  71. the parameter list.  They can be separated by commas or semicolons, but
  72. the last one must end with a semicolon, which is followed by the "real"
  73. parameter declarations.  Each forward declaration must match a "real"
  74. declaration in parameter name and data type.
  75. File: gcc.info,  Node: Macro Varargs,  Next: Subscripting,  Prev: Variable Length,  Up: C Extensions
  76. Macros with Variable Numbers of Arguments
  77. =========================================
  78.    In GNU C, a macro can accept a variable number of arguments, much as
  79. a function can.  The syntax for defining the macro looks much like that
  80. used for a function.  Here is an example:
  81.      #define eprintf(format, args...)  \
  82.       fprintf (stderr, format , ## args)
  83.    Here `args' is a "rest argument": it takes in zero or more
  84. arguments, as many as the call contains.  All of them plus the commas
  85. between them form the value of `args', which is substituted into the
  86. macro body where `args' is used.  Thus, we have this expansion:
  87.      eprintf ("%s:%d: ", input_file_name, line_number)
  88.      ==>
  89.      fprintf (stderr, "%s:%d: " , input_file_name, line_number)
  90. Note that the comma after the string constant comes from the definition
  91. of `eprintf', whereas the last comma comes from the value of `args'.
  92.    The reason for using `##' is to handle the case when `args' matches
  93. no arguments at all.  In this case, `args' has an empty value.  In this
  94. case, the second comma in the definition becomes an embarrassment: if
  95. it got through to the expansion of the macro, we would get something
  96. like this:
  97.      fprintf (stderr, "success!\n" , )
  98. which is invalid C syntax.  `##' gets rid of the comma, so we get the
  99. following instead:
  100.      fprintf (stderr, "success!\n")
  101.    This is a special feature of the GNU C preprocessor: `##' before a
  102. rest argument that is empty discards the preceding sequence of
  103. non-whitespace characters from the macro definition.  (If another macro
  104. argument precedes, none of it is discarded.)
  105.    It might be better to discard the last preprocessor token instead of
  106. the last preceding sequence of non-whitespace characters; in fact, we
  107. may someday change this feature to do so.  We advise you to write the
  108. macro definition so that the preceding sequence of non-whitespace
  109. characters is just a single token, so that the meaning will not change
  110. if we change the definition of this feature.
  111. File: gcc.info,  Node: Subscripting,  Next: Pointer Arith,  Prev: Macro Varargs,  Up: C Extensions
  112. Non-Lvalue Arrays May Have Subscripts
  113. =====================================
  114.    Subscripting is allowed on arrays that are not lvalues, even though
  115. the unary `&' operator is not.  For example, this is valid in GNU C
  116. though not valid in other C dialects:
  117.      struct foo {int a[4];};
  118.      
  119.      struct foo f();
  120.      
  121.      bar (int index)
  122.      {
  123.        return f().a[index];
  124.      }
  125. File: gcc.info,  Node: Pointer Arith,  Next: Initializers,  Prev: Subscripting,  Up: C Extensions
  126. Arithmetic on `void'- and Function-Pointers
  127. ===========================================
  128.    In GNU C, addition and subtraction operations are supported on
  129. pointers to `void' and on pointers to functions.  This is done by
  130. treating the size of a `void' or of a function as 1.
  131.    A consequence of this is that `sizeof' is also allowed on `void' and
  132. on function types, and returns 1.
  133.    The option `-Wpointer-arith' requests a warning if these extensions
  134. are used.
  135. File: gcc.info,  Node: Initializers,  Next: Constructors,  Prev: Pointer Arith,  Up: C Extensions
  136. Non-Constant Initializers
  137. =========================
  138.    The elements of an aggregate initializer for an automatic variable
  139. are not required to be constant expressions in GNU C.  Here is an
  140. example of an initializer with run-time varying elements:
  141.      foo (float f, float g)
  142.      {
  143.        float beat_freqs[2] = { f-g, f+g };
  144.        ...
  145.      }
  146. File: gcc.info,  Node: Constructors,  Next: Labeled Elements,  Prev: Initializers,  Up: C Extensions
  147. Constructor Expressions
  148. =======================
  149.    GNU C supports constructor expressions.  A constructor looks like a
  150. cast containing an initializer.  Its value is an object of the type
  151. specified in the cast, containing the elements specified in the
  152. initializer.
  153.    Usually, the specified type is a structure.  Assume that `struct
  154. foo' and `structure' are declared as shown:
  155.      struct foo {int a; char b[2];} structure;
  156. Here is an example of constructing a `struct foo' with a constructor:
  157.      structure = ((struct foo) {x + y, 'a', 0});
  158. This is equivalent to writing the following:
  159.      {
  160.        struct foo temp = {x + y, 'a', 0};
  161.        structure = temp;
  162.      }
  163.    You can also construct an array.  If all the elements of the
  164. constructor are (made up of) simple constant expressions, suitable for
  165. use in initializers, then the constructor is an lvalue and can be
  166. coerced to a pointer to its first element, as shown here:
  167.      char **foo = (char *[]) { "x", "y", "z" };
  168.    Array constructors whose elements are not simple constants are not
  169. very useful, because the constructor is not an lvalue.  There are only
  170. two valid ways to use it: to subscript it, or initialize an array
  171. variable with it.  The former is probably slower than a `switch'
  172. statement, while the latter does the same thing an ordinary C
  173. initializer would do.  Here is an example of subscripting an array
  174. constructor:
  175.      output = ((int[]) { 2, x, 28 }) [input];
  176.    Constructor expressions for scalar types and union types are is also
  177. allowed, but then the constructor expression is equivalent to a cast.
  178. File: gcc.info,  Node: Labeled Elements,  Next: Cast to Union,  Prev: Constructors,  Up: C Extensions
  179. Labeled Elements in Initializers
  180. ================================
  181.    Standard C requires the elements of an initializer to appear in a
  182. fixed order, the same as the order of the elements in the array or
  183. structure being initialized.
  184.    In GNU C you can give the elements in any order, specifying the array
  185. indices or structure field names they apply to.
  186.    To specify an array index, write `[INDEX]' before the element value.
  187. For example,
  188.      int a[6] = { [4] 29, [2] 15 };
  189. is equivalent to
  190.      int a[6] = { 0, 0, 15, 0, 29, 0 };
  191. The index values must be constant expressions, even if the array being
  192. initialized is automatic.
  193.    In a structure initializer, specify the name of a field to initialize
  194. with `FIELDNAME:' before the element value.  For example, given the
  195. following structure,
  196.      struct point { int x, y; };
  197. the following initialization
  198.      struct point p = { y: yvalue, x: xvalue };
  199. is equivalent to
  200.      struct point p = { xvalue, yvalue };
  201.    You can also use an element label when initializing a union, to
  202. specify which element of the union should be used.  For example,
  203.      union foo { int i; double d; };
  204.      
  205.      union foo f = { d: 4 };
  206. will convert 4 to a `double' to store it in the union using the second
  207. element.  By contrast, casting 4 to type `union foo' would store it
  208. into the union as the integer `i', since it is an integer.  (*Note Cast
  209. to Union::.)
  210.    You can combine this technique of naming elements with ordinary C
  211. initialization of successive elements.  Each initializer element that
  212. does not have a label applies to the next consecutive element of the
  213. array or structure.  For example,
  214.      int a[6] = { [1] v1, v2, [4] v4 };
  215. is equivalent to
  216.      int a[6] = { 0, v1, v2, 0, v4, 0 };
  217.    Labeling the elements of an array initializer is especially useful
  218. when the indices are characters or belong to an `enum' type.  For
  219. example:
  220.      int whitespace[256]
  221.        = { [' '] 1, ['\t'] 1, ['\h'] 1,
  222.            ['\f'] 1, ['\n'] 1, ['\r'] 1 };
  223. File: gcc.info,  Node: Case Ranges,  Next: Function Attributes,  Prev: Cast to Union,  Up: C Extensions
  224. Case Ranges
  225. ===========
  226.    You can specify a range of consecutive values in a single `case'
  227. label, like this:
  228.      case LOW ... HIGH:
  229. This has the same effect as the proper number of individual `case'
  230. labels, one for each integer value from LOW to HIGH, inclusive.
  231.    This feature is especially useful for ranges of ASCII character
  232. codes:
  233.      case 'A' ... 'Z':
  234.    *Be careful:* Write spaces around the `...', for otherwise it may be
  235. parsed wrong when you use it with integer values.  For example, write
  236. this:
  237.      case 1 ... 5:
  238. rather than this:
  239.      case 1...5:
  240.      *Warning to C++ users:* When compiling C++, you must write two dots
  241.      `..' rather than three to specify a range in case statements, thus:
  242.           case 'A' .. 'Z':
  243.      This is an anachronism in the GNU C++ front end, and will be
  244.      rectified in a future release.
  245. File: gcc.info,  Node: Cast to Union,  Next: Case Ranges,  Prev: Labeled Elements,  Up: C Extensions
  246. Cast to a Union Type
  247. ====================
  248.    A cast to union type is similar to other casts, except that the type
  249. specified is a union type.  You can specify the type either with `union
  250. TAG' or with a typedef name.  A cast to union is actually a constructor
  251. though, not a cast, and hence does not yield an lvalue like normal
  252. casts.  (*Note Constructors::.)
  253.    The types that may be cast to the union type are those of the members
  254. of the union.  Thus, given the following union and variables:
  255.      union foo { int i; double d; };
  256.      int x;
  257.      double y;
  258. both `x' and `y' can be cast to type `union' foo.
  259.    Using the cast as the right-hand side of an assignment to a variable
  260. of union type is equivalent to storing in a member of the union:
  261.      union foo u;
  262.      ...
  263.      u = (union foo) x  ==  u.i = x
  264.      u = (union foo) y  ==  u.d = y
  265.    You can also use the union cast as a function argument:
  266.      void hack (union foo);
  267.      ...
  268.      hack ((union foo) x);
  269. File: gcc.info,  Node: Function Attributes,  Next: Function Prototypes,  Prev: Case Ranges,  Up: C Extensions
  270. Declaring Attributes of Functions
  271. =================================
  272.    In GNU C, you declare certain things about functions called in your
  273. program which help the compiler optimize function calls.
  274.    A few standard library functions, such as `abort' and `exit', cannot
  275. return.  GNU CC knows this automatically.  Some programs define their
  276. own functions that never return.  You can declare them `volatile' to
  277. tell the compiler this fact.  For example,
  278.      typedef void voidfn ();
  279.      
  280.      volatile voidfn fatal;
  281.      
  282.      void
  283.      fatal (...)
  284.      {
  285.        ... /* Print error message. */ ...
  286.        exit (1);
  287.      }
  288.    The `volatile' keyword tells the compiler to assume that `fatal'
  289. cannot return.  It can then optimize without regard to what would
  290. happen if `fatal' ever did return.  This makes slightly better code.
  291. More importantly, it helps avoid spurious warnings of uninitialized
  292. variables.
  293.    Do not assume that registers saved by the calling function are
  294. restored before calling the `volatile' function.
  295.    It does not make sense for a `volatile' function to have a return
  296. type other than `void'.
  297.    Many functions do not examine any values except their arguments, and
  298. have no effects except the return value.  Such a function can be subject
  299. to common subexpression elimination and loop optimization just as an
  300. arithmetic operator would be.  These functions should be declared
  301. `const'.  For example,
  302.      typedef int intfn ();
  303.      
  304.      extern const intfn square;
  305. says that the hypothetical function `square' is safe to call fewer
  306. times than the program says.
  307.    Note that a function that has pointer arguments and examines the data
  308. pointed to must *not* be declared `const'.  Likewise, a function that
  309. calls a non-`const' function usually must not be `const'.  It does not
  310. make sense for a `const' function to return `void'.
  311.    The examples above use `typedef' because that is the only way to
  312. declare a function `const' or `volatile'.  A declaration like this:
  313.      extern const int square ();
  314. does not have this effect; it says that the return type of `square' is
  315. `const', not `square' itself.
  316.    Some people object to this feature, suggesting that ANSI C's
  317. `#pragma' should be used instead.  There are two reasons for not doing
  318. this.
  319.   1. It is impossible to generate `#pragma' commands from a macro.
  320.   2. There is no telling what the same `#pragma' might mean in another
  321.      compiler.
  322.    These two reasons apply to almost any application that might be
  323. proposed for `#pragma'.  It is basically a mistake to use `#pragma' for
  324. *anything*.
  325.    The keyword `__attribute__' allows you to specify special attributes
  326. when making a declaration.  This keyword is followed by an attribute
  327. specification inside double parentheses.  One attribute, `format', is
  328. currently defined for functions.  Others are implemented for variables
  329. and structure fields (*note Variable Attributes::.).
  330. `format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)'
  331.      The `format' attribute specifies that a function takes `printf' or
  332.      `scanf' style arguments which should be type-checked against a
  333.      format string.  For example, the declaration:
  334.           extern int
  335.           my_printf (void *my_object, const char *my_format, ...)
  336.                 __attribute__ ((format (printf, 2, 3)));
  337.      causes the compiler to check the arguments in calls to `my_printf'
  338.      for consistency with the `printf' style format string argument
  339.      `my_format'.
  340.      The parameter ARCHETYPE determines how the format string is
  341.      interpreted, and should be either `printf' or `scanf'.  The
  342.      parameter STRING-INDEX specifies which argument is the format
  343.      string argument (starting from 1), while FIRST-TO-CHECK is the
  344.      number of the first argument to check against the format string.
  345.      For functions where the arguments are not available to be checked
  346.      (such as `vprintf'), specify the third parameter as zero.  In this
  347.      case the compiler only checks the format string for consistency.
  348.      In the example above, the format string (`my_format') is the second
  349.      argument of the function `my_print', and the arguments to check
  350.      start with the third argument, so the correct parameters for the
  351.      format attribute are 2 and 3.
  352.      The `format' attribute allows you to identify your own functions
  353.      which take format strings as arguments, so that GNU CC can check
  354.      the calls to these functions for errors.  The compiler always
  355.      checks formats for the ANSI library functions `printf', `fprintf',
  356.      `sprintf', `scanf', `fscanf', `sscanf', `vprintf', `vfprintf' and
  357.      `vsprintf' whenever such warnings are requested (using
  358.      `-Wformat'), so there is no need to modify the header file
  359.      `stdio.h'.
  360. File: gcc.info,  Node: Function Prototypes,  Next: Dollar Signs,  Prev: Function Attributes,  Up: C Extensions
  361. Prototypes and Old-Style Function Definitions
  362. =============================================
  363.    GNU C extends ANSI C to allow a function prototype to override a
  364. later old-style non-prototype definition.  Consider the following
  365. example:
  366.      /* Use prototypes unless the compiler is old-fashioned.  */
  367.      #if __STDC__
  368.      #define P(x) x
  369.      #else
  370.      #define P(x) ()
  371.      #endif
  372.      
  373.      /* Prototype function declaration.  */
  374.      int isroot P((uid_t));
  375.      
  376.      /* Old-style function definition.  */
  377.      int
  378.      isroot (x)   /* ??? lossage here ??? */
  379.           uid_t x;
  380.      {
  381.        return x == 0;
  382.      }
  383.    Suppose the type `uid_t' happens to be `short'.  ANSI C does not
  384. allow this example, because subword arguments in old-style
  385. non-prototype definitions are promoted.  Therefore in this example the
  386. function definition's argument is really an `int', which does not match
  387. the prototype argument type of `short'.
  388.    This restriction of ANSI C makes it hard to write code that is
  389. portable to traditional C compilers, because the programmer does not
  390. know whether the `uid_t' type is `short', `int', or `long'.  Therefore,
  391. in cases like these GNU C allows a prototype to override a later
  392. old-style definition.  More precisely, in GNU C, a function prototype
  393. argument type overrides the argument type specified by a later
  394. old-style definition if the former type is the same as the latter type
  395. before promotion.  Thus in GNU C the above example is equivalent to the
  396. following:
  397.      int isroot (uid_t);
  398.      
  399.      int
  400.      isroot (uid_t x)
  401.      {
  402.        return x == 0;
  403.      }
  404. File: gcc.info,  Node: Dollar Signs,  Next: Character Escapes,  Prev: Function Prototypes,  Up: C Extensions
  405. Dollar Signs in Identifier Names
  406. ================================
  407.    In GNU C, you may use dollar signs in identifier names.  This is
  408. because many traditional C implementations allow such identifiers.
  409.    On some machines, dollar signs are allowed in identifiers if you
  410. specify `-traditional'.  On a few systems they are allowed by default,
  411. even if you do not use `-traditional'.  But they are never allowed if
  412. you specify `-ansi'.
  413.    There are certain ANSI C programs (obscure, to be sure) that would
  414. compile incorrectly if dollar signs were permitted in identifiers.  For
  415. example:
  416.      #define foo(a) #a
  417.      #define lose(b) foo (b)
  418.      #define test$
  419.      lose (test)
  420. File: gcc.info,  Node: Character Escapes,  Next: Variable Attributes,  Prev: Dollar Signs,  Up: C Extensions
  421. The Character ESC in Constants
  422. ==============================
  423.    You can use the sequence `\e' in a string or character constant to
  424. stand for the ASCII character ESC.
  425. File: gcc.info,  Node: Alignment,  Next: Inline,  Prev: Variable Attributes,  Up: C Extensions
  426. Inquiring on Alignment of Types or Variables
  427. ============================================
  428.    The keyword `__alignof__' allows you to inquire about how an object
  429. is aligned, or the minimum alignment usually required by a type.  Its
  430. syntax is just like `sizeof'.
  431.    For example, if the target machine requires a `double' value to be
  432. aligned on an 8-byte boundary, then `__alignof__ (double)' is 8.  This
  433. is true on many RISC machines.  On more traditional machine designs,
  434. `__alignof__ (double)' is 4 or even 2.
  435.    Some machines never actually require alignment; they allow reference
  436. to any data type even at an odd addresses.  For these machines,
  437. `__alignof__' reports the *recommended* alignment of a type.
  438.    When the operand of `__alignof__' is an lvalue rather than a type,
  439. the value is the largest alignment that the lvalue is known to have.
  440. It may have this alignment as a result of its data type, or because it
  441. is part of a structure and inherits alignment from that structure.  For
  442. example, after this declaration:
  443.      struct foo { int x; char y; } foo1;
  444. the value of `__alignof__ (foo1.y)' is probably 2 or 4, the same as
  445. `__alignof__ (int)', even though the data type of `foo1.y' does not
  446. itself demand any alignment.
  447.    A related feature which lets you specify the alignment of an object
  448. is `__attribute__ ((aligned (ALIGNMENT)))'; see the following section.
  449. File: gcc.info,  Node: Variable Attributes,  Next: Alignment,  Prev: Character Escapes,  Up: C Extensions
  450. Specifying Attributes of Variables
  451. ==================================
  452.    The keyword `__attribute__' allows you to specify special attributes
  453. of variables or structure fields.  This keyword is followed by an
  454. attribute specification inside double parentheses.  Four attributes are
  455. currently defined: `aligned', `format', `mode' and `packed'.  `format'
  456. is used for functions, and thus not documented here; see *Note Function
  457. Attributes::.
  458. `aligned (ALIGNMENT)'
  459.      This attribute specifies a minimum alignment for the variable or
  460.      structure field, measured in bytes.  For example, the declaration:
  461.           int x __attribute__ ((aligned (16))) = 0;
  462.      causes the compiler to allocate the global variable `x' on a
  463.      16-byte boundary.  On a 68040, this could be used in conjunction
  464.      with an `asm' expression to access the `move16' instruction which
  465.      requires 16-byte aligned operands.
  466.      You can also specify the alignment of structure fields.  For
  467.      example, to create a double-word aligned `int' pair, you could
  468.      write:
  469.           struct foo { int x[2] __attribute__ ((aligned (8))); };
  470.      This is an alternative to creating a union with a `double' member
  471.      that forces the union to be double-word aligned.
  472.      It is not possible to specify the alignment of functions; the
  473.      alignment of functions is determined by the machine's requirements
  474.      and cannot be changed.  You cannot specify alignment for a typedef
  475.      name because such a name is just an alias, not a distinct type.
  476.      The `aligned' attribute can only increase the alignment; but you
  477.      can decrease it by specifying `packed' as well.  See below.
  478.      The linker of your operating system imposes a maximum alignment.
  479.      If the linker aligns each object file on a four byte boundary,
  480.      then it is beyond the compiler's power to cause anything to be
  481.      aligned to a larger boundary than that.  For example, if  the
  482.      linker happens to put this object file at address 136 (eight more
  483.      than a multiple of 64), then the compiler cannot guarantee an
  484.      alignment of more than 8 just by aligning variables in the object
  485.      file.
  486. `mode (MODE)'
  487.      This attribute specifies the data type for the
  488.      declaration--whichever type corresponds to the mode MODE.  This in
  489.      effect lets you request an integer or floating point type
  490.      according to its width.
  491. `packed'
  492.      The `packed' attribute specifies that a variable or structure field
  493.      should have the smallest possible alignment--one byte for a
  494.      variable, and one bit for a field, unless you specify a larger
  495.      value with the `aligned' attribute.
  496.    To specify multiple attributes, separate them by commas within the
  497. double parentheses: for example, `__attribute__ ((aligned (16),
  498. packed))'.
  499. File: gcc.info,  Node: Inline,  Next: Extended Asm,  Prev: Alignment,  Up: C Extensions
  500. An Inline Function is As Fast As a Macro
  501. ========================================
  502.    By declaring a function `inline', you can direct GNU CC to integrate
  503. that function's code into the code for its callers.  This makes
  504. execution faster by eliminating the function-call overhead; in
  505. addition, if any of the actual argument values are constant, their known
  506. values may permit simplifications at compile time so that not all of the
  507. inline function's code needs to be included.  The effect on code size is
  508. less predictable; object code may be larger or smaller with function
  509. inlining, depending on the particular case.  Inlining of functions is an
  510. optimization and it really "works" only in optimizing compilation.  If
  511. you don't use `-O', no function is really inline.
  512.    To declare a function inline, use the `inline' keyword in its
  513. declaration, like this:
  514.      inline int
  515.      inc (int *a)
  516.      {
  517.        (*a)++;
  518.      }
  519.    (If you are writing a header file to be included in ANSI C programs,
  520. write `__inline__' instead of `inline'.  *Note Alternate Keywords::.)
  521.    You can also make all "simple enough" functions inline with the
  522. option `-finline-functions'.  Note that certain usages in a function
  523. definition can make it unsuitable for inline substitution.
  524.    For C++ programs, GNU CC automatically inlines member functions even
  525. if they are not explicitly declared `inline'.  (You can override this
  526. with `-fno-default-inline'; *note Options Controlling C++ Dialect: C++
  527. Dialect Options..)
  528.    When a function is both inline and `static', if all calls to the
  529. function are integrated into the caller, and the function's address is
  530. never used, then the function's own assembler code is never referenced.
  531. In this case, GNU CC does not actually output assembler code for the
  532. function, unless you specify the option `-fkeep-inline-functions'.
  533. Some calls cannot be integrated for various reasons (in particular,
  534. calls that precede the function's definition cannot be integrated, and
  535. neither can recursive calls within the definition).  If there is a
  536. nonintegrated call, then the function is compiled to assembler code as
  537. usual.  The function must also be compiled as usual if the program
  538. refers to its address, because that can't be inlined.
  539.    When an inline function is not `static', then the compiler must
  540. assume that there may be calls from other source files; since a global
  541. symbol can be defined only once in any program, the function must not
  542. be defined in the other source files, so the calls therein cannot be
  543. integrated.  Therefore, a non-`static' inline function is always
  544. compiled on its own in the usual fashion.
  545.    If you specify both `inline' and `extern' in the function
  546. definition, then the definition is used only for inlining.  In no case
  547. is the function compiled on its own, not even if you refer to its
  548. address explicitly.  Such an address becomes an external reference, as
  549. if you had only declared the function, and had not defined it.
  550.    This combination of `inline' and `extern' has almost the effect of a
  551. macro.  The way to use it is to put a function definition in a header
  552. file with these keywords, and put another copy of the definition
  553. (lacking `inline' and `extern') in a library file.  The definition in
  554. the header file will cause most calls to the function to be inlined.
  555. If any uses of the function remain, they will refer to the single copy
  556. in the library.
  557.    GNU C does not inline any functions when not optimizing.  It is not
  558. clear whether it is better to inline or not, in this case, but we found
  559. that a correct implementation when not optimizing was difficult.  So we
  560. did the easy thing, and turned it off.
  561. File: gcc.info,  Node: Extended Asm,  Next: Asm Labels,  Prev: Inline,  Up: C Extensions
  562. Assembler Instructions with C Expression Operands
  563. =================================================
  564.    In an assembler instruction using `asm', you can now specify the
  565. operands of the instruction using C expressions.  This means no more
  566. guessing which registers or memory locations will contain the data you
  567. want to use.
  568.    You must specify an assembler instruction template much like what
  569. appears in a machine description, plus an operand constraint string for
  570. each operand.
  571.    For example, here is how to use the 68881's `fsinx' instruction:
  572.      asm ("fsinx %1,%0" : "=f" (result) : "f" (angle));
  573. Here `angle' is the C expression for the input operand while `result'
  574. is that of the output operand.  Each has `"f"' as its operand
  575. constraint, saying that a floating point register is required.  The `='
  576. in `=f' indicates that the operand is an output; all output operands'
  577. constraints must use `='.  The constraints use the same language used
  578. in the machine description (*note Constraints::.).
  579.    Each operand is described by an operand-constraint string followed
  580. by the C expression in parentheses.  A colon separates the assembler
  581. template from the first output operand, and another separates the last
  582. output operand from the first input, if any.  Commas separate output
  583. operands and separate inputs.  The total number of operands is limited
  584. to ten or to the maximum number of operands in any instruction pattern
  585. in the machine description, whichever is greater.
  586.    If there are no output operands, and there are input operands, then
  587. there must be two consecutive colons surrounding the place where the
  588. output operands would go.
  589.    Output operand expressions must be lvalues; the compiler can check
  590. this.  The input operands need not be lvalues.  The compiler cannot
  591. check whether the operands have data types that are reasonable for the
  592. instruction being executed.  It does not parse the assembler
  593. instruction template and does not know what it means, or whether it is
  594. valid assembler input.  The extended `asm' feature is most often used
  595. for machine instructions that the compiler itself does not know exist.
  596.    The output operands must be write-only; GNU CC will assume that the
  597. values in these operands before the instruction are dead and need not be
  598. generated.  Extended asm does not support input-output or read-write
  599. operands.  For this reason, the constraint character `+', which
  600. indicates such an operand, may not be used.
  601.    When the assembler instruction has a read-write operand, or an
  602. operand in which only some of the bits are to be changed, you must
  603. logically split its function into two separate operands, one input
  604. operand and one write-only output operand.  The connection between them
  605. is expressed by constraints which say they need to be in the same
  606. location when the instruction executes.  You can use the same C
  607. expression for both operands, or different expressions.  For example,
  608. here we write the (fictitious) `combine' instruction with `bar' as its
  609. read-only source operand and `foo' as its read-write destination:
  610.      asm ("combine %2,%0" : "=r" (foo) : "0" (foo), "g" (bar));
  611. The constraint `"0"' for operand 1 says that it must occupy the same
  612. location as operand 0.  A digit in constraint is allowed only in an
  613. input operand, and it must refer to an output operand.
  614.    Only a digit in the constraint can guarantee that one operand will
  615. be in the same place as another.  The mere fact that `foo' is the value
  616. of both operands is not enough to guarantee that they will be in the
  617. same place in the generated assembler code.  The following would not
  618. work:
  619.      asm ("combine %2,%0" : "=r" (foo) : "r" (foo), "g" (bar));
  620.    Various optimizations or reloading could cause operands 0 and 1 to
  621. be in different registers; GNU CC knows no reason not to do so.  For
  622. example, the compiler might find a copy of the value of `foo' in one
  623. register and use it for operand 1, but generate the output operand 0 in
  624. a different register (copying it afterward to `foo''s own address).  Of
  625. course, since the register for operand 1 is not even mentioned in the
  626. assembler code, the result will not work, but GNU CC can't tell that.
  627.    Some instructions clobber specific hard registers.  To describe
  628. this, write a third colon after the input operands, followed by the
  629. names of the clobbered hard registers (given as strings).  Here is a
  630. realistic example for the Vax:
  631.      asm volatile ("movc3 %0,%1,%2"
  632.                    : /* no outputs */
  633.                    : "g" (from), "g" (to), "g" (count)
  634.                    : "r0", "r1", "r2", "r3", "r4", "r5");
  635.    If you refer to a particular hardware register from the assembler
  636. code, then you will probably have to list the register after the third
  637. colon to tell the compiler that the register's value is modified.  In
  638. many assemblers, the register names begin with `%'; to produce one `%'
  639. in the assembler code, you must write `%%' in the input.
  640.    If your assembler instruction can alter the condition code register,
  641. add `cc' to the list of clobbered registers.  GNU CC on some machines
  642. represents the condition codes as a specific hardware register; `cc'
  643. serves to name this register.  On other machines, the condition code is
  644. handled differently, and specifying `cc' has no effect.  But it is
  645. valid no matter what the machine.
  646.    If your assembler instruction modifies memory in an unpredictable
  647. fashion, add `memory' to the list of clobbered registers.  This will
  648. cause GNU CC to not keep memory values cached in registers across the
  649. assembler instruction.
  650.    You can put multiple assembler instructions together in a single
  651. `asm' template, separated either with newlines (written as `\n') or with
  652. semicolons if the assembler allows such semicolons.  The GNU assembler
  653. allows semicolons and all Unix assemblers seem to do so.  The input
  654. operands are guaranteed not to use any of the clobbered registers, and
  655. neither will the output operands' addresses, so you can read and write
  656. the clobbered registers as many times as you like.  Here is an example
  657. of multiple instructions in a template; it assumes that the subroutine
  658. `_foo' accepts arguments in registers 9 and 10:
  659.      asm ("movl %0,r9;movl %1,r10;call _foo"
  660.           : /* no outputs */
  661.           : "g" (from), "g" (to)
  662.           : "r9", "r10");
  663.    Unless an output operand has the `&' constraint modifier, GNU CC may
  664. allocate it in the same register as an unrelated input operand, on the
  665. assumption that the inputs are consumed before the outputs are produced.
  666. This assumption may be false if the assembler code actually consists of
  667. more than one instruction.  In such a case, use `&' for each output
  668. operand that may not overlap an input.  *Note Modifiers::.
  669.    If you want to test the condition code produced by an assembler
  670. instruction, you must include a branch and a label in the `asm'
  671. construct, as follows:
  672.      asm ("clr %0;frob %1;beq 0f;mov #1,%0;0:"
  673.           : "g" (result)
  674.           : "g" (input));
  675. This assumes your assembler supports local labels, as the GNU assembler
  676. and most Unix assemblers do.
  677.    Speaking of labels, jumps from one `asm' to another are not
  678. supported.  The compiler's optimizers do not know about these jumps,
  679. and therefore they cannot take account of them when deciding how to
  680. optimize.
  681.    Usually the most convenient way to use these `asm' instructions is to
  682. encapsulate them in macros that look like functions.  For example,
  683.      #define sin(x)       \
  684.      ({ double __value, __arg = (x);   \
  685.         asm ("fsinx %1,%0": "=f" (__value): "f" (__arg));  \
  686.         __value; })
  687. Here the variable `__arg' is used to make sure that the instruction
  688. operates on a proper `double' value, and to accept only those arguments
  689. `x' which can convert automatically to a `double'.
  690.    Another way to make sure the instruction operates on the correct
  691. data type is to use a cast in the `asm'.  This is different from using a
  692. variable `__arg' in that it converts more different types.  For
  693. example, if the desired type were `int', casting the argument to `int'
  694. would accept a pointer with no complaint, while assigning the argument
  695. to an `int' variable named `__arg' would warn about using a pointer
  696. unless the caller explicitly casts it.
  697.    If an `asm' has output operands, GNU CC assumes for optimization
  698. purposes that the instruction has no side effects except to change the
  699. output operands.  This does not mean that instructions with a side
  700. effect cannot be used, but you must be careful, because the compiler
  701. may eliminate them if the output operands aren't used, or move them out
  702. of loops, or replace two with one if they constitute a common
  703. subexpression.  Also, if your instruction does have a side effect on a
  704. variable that otherwise appears not to change, the old value of the
  705. variable may be reused later if it happens to be found in a register.
  706.    You can prevent an `asm' instruction from being deleted, moved
  707. significantly, or combined, by writing the keyword `volatile' after the
  708. `asm'.  For example:
  709.      #define set_priority(x)  \
  710.      asm volatile ("set_priority %0": /* no outputs */ : "g" (x))
  711. An instruction without output operands will not be deleted or moved
  712. significantly, regardless, unless it is unreachable.
  713.    Note that even a volatile `asm' instruction can be moved in ways
  714. that appear insignificant to the compiler, such as across jump
  715. instructions.  You can't expect a sequence of volatile `asm'
  716. instructions to remain perfectly consecutive.  If you want consecutive
  717. output, use a single `asm'.
  718.    It is a natural idea to look for a way to give access to the
  719. condition code left by the assembler instruction.  However, when we
  720. attempted to implement this, we found no way to make it work reliably.
  721. The problem is that output operands might need reloading, which would
  722. result in additional following "store" instructions.  On most machines,
  723. these instructions would alter the condition code before there was time
  724. to test it.  This problem doesn't arise for ordinary "test" and
  725. "compare" instructions because they don't have any output operands.
  726.    If you are writing a header file that should be includable in ANSI C
  727. programs, write `__asm__' instead of `asm'.  *Note Alternate Keywords::.
  728. File: gcc.info,  Node: Asm Labels,  Next: Explicit Reg Vars,  Prev: Extended Asm,  Up: C Extensions
  729. Controlling Names Used in Assembler Code
  730. ========================================
  731.    You can specify the name to be used in the assembler code for a C
  732. function or variable by writing the `asm' (or `__asm__') keyword after
  733. the declarator as follows:
  734.      int foo asm ("myfoo") = 2;
  735. This specifies that the name to be used for the variable `foo' in the
  736. assembler code should be `myfoo' rather than the usual `_foo'.
  737.    On systems where an underscore is normally prepended to the name of
  738. a C function or variable, this feature allows you to define names for
  739. the linker that do not start with an underscore.
  740.    You cannot use `asm' in this way in a function *definition*; but you
  741. can get the same effect by writing a declaration for the function
  742. before its definition and putting `asm' there, like this:
  743.      extern func () asm ("FUNC");
  744.      
  745.      func (x, y)
  746.           int x, y;
  747.      ...
  748.    It is up to you to make sure that the assembler names you choose do
  749. not conflict with any other assembler symbols.  Also, you must not use a
  750. register name; that would produce completely invalid assembler code.
  751. GNU CC does not as yet have the ability to store static variables in
  752. registers.  Perhaps that will be added.
  753. File: gcc.info,  Node: Explicit Reg Vars,  Next: Alternate Keywords,  Prev: Asm Labels,  Up: C Extensions
  754. Variables in Specified Registers
  755. ================================
  756.    GNU C allows you to put a few global variables into specified
  757. hardware registers.  You can also specify the register in which an
  758. ordinary register variable should be allocated.
  759.    * Global register variables reserve registers throughout the program.
  760.      This may be useful in programs such as programming language
  761.      interpreters which have a couple of global variables that are
  762.      accessed very often.
  763.    * Local register variables in specific registers do not reserve the
  764.      registers.  The compiler's data flow analysis is capable of
  765.      determining where the specified registers contain live values, and
  766.      where they are available for other uses.
  767.      These local variables are sometimes convenient for use with the
  768.      extended `asm' feature (*note Extended Asm::.), if you want to
  769.      write one output of the assembler instruction directly into a
  770.      particular register.  (This will work provided the register you
  771.      specify fits the constraints specified for that operand in the
  772.      `asm'.)
  773. * Menu:
  774. * Global Reg Vars::
  775. * Local Reg Vars::
  776. File: gcc.info,  Node: Global Reg Vars,  Next: Local Reg Vars,  Up: Explicit Reg Vars
  777. Defining Global Register Variables
  778. ----------------------------------
  779.    You can define a global register variable in GNU C like this:
  780.      register int *foo asm ("a5");
  781. Here `a5' is the name of the register which should be used.  Choose a
  782. register which is normally saved and restored by function calls on your
  783. machine, so that library routines will not clobber it.
  784.    Naturally the register name is cpu-dependent, so you would need to
  785. conditionalize your program according to cpu type.  The register `a5'
  786. would be a good choice on a 68000 for a variable of pointer type.  On
  787. machines with register windows, be sure to choose a "global" register
  788. that is not affected magically by the function call mechanism.
  789.    In addition, operating systems on one type of cpu may differ in how
  790. they name the registers; then you would need additional conditionals.
  791. For example, some 68000 operating systems call this register `%a5'.
  792.    Eventually there may be a way of asking the compiler to choose a
  793. register automatically, but first we need to figure out how it should
  794. choose and how to enable you to guide the choice.  No solution is
  795. evident.
  796.    Defining a global register variable in a certain register reserves
  797. that register entirely for this use, at least within the current
  798. compilation.  The register will not be allocated for any other purpose
  799. in the functions in the current compilation.  The register will not be
  800. saved and restored by these functions.  Stores into this register are
  801. never deleted even if they would appear to be dead, but references may
  802. be deleted or moved or simplified.
  803.    It is not safe to access the global register variables from signal
  804. handlers, or from more than one thread of control, because the system
  805. library routines may temporarily use the register for other things
  806. (unless you recompile them specially for the task at hand).
  807.    It is not safe for one function that uses a global register variable
  808. to call another such function `foo' by way of a third function `lose'
  809. that was compiled without knowledge of this variable (i.e. in a
  810. different source file in which the variable wasn't declared).  This is
  811. because `lose' might save the register and put some other value there.
  812. For example, you can't expect a global register variable to be
  813. available in the comparison-function that you pass to `qsort', since
  814. `qsort' might have put something else in that register.  (If you are
  815. prepared to recompile `qsort' with the same global register variable,
  816. you can solve this problem.)
  817.    If you want to recompile `qsort' or other source files which do not
  818. actually use your global register variable, so that they will not use
  819. that register for any other purpose, then it suffices to specify the
  820. compiler option `-ffixed-REG'.  You need not actually add a global
  821. register declaration to their source code.
  822.    A function which can alter the value of a global register variable
  823. cannot safely be called from a function compiled without this variable,
  824. because it could clobber the value the caller expects to find there on
  825. return.  Therefore, the function which is the entry point into the part
  826. of the program that uses the global register variable must explicitly
  827. save and restore the value which belongs to its caller.
  828.    On most machines, `longjmp' will restore to each global register
  829. variable the value it had at the time of the `setjmp'.  On some
  830. machines, however, `longjmp' will not change the value of global
  831. register variables.  To be portable, the function that called `setjmp'
  832. should make other arrangements to save the values of the global register
  833. variables, and to restore them in a `longjmp'.  This way, the same
  834. thing will happen regardless of what `longjmp' does.
  835.    All global register variable declarations must precede all function
  836. definitions.  If such a declaration could appear after function
  837. definitions, the declaration would be too late to prevent the register
  838. from being used for other purposes in the preceding functions.
  839.    Global register variables may not have initial values, because an
  840. executable file has no means to supply initial contents for a register.
  841.    On the Sparc, there are reports that g3 ... g7 are suitable
  842. registers, but certain library functions, such as `getwd', as well as
  843. the subroutines for division and remainder, modify g3 and g4.  g1 and
  844. g2 are local temporaries.
  845.    On the 68000, a2 ... a5 should be suitable, as should d2 ... d7.  Of
  846. course, it will not do to use more than a few of those.
  847. File: gcc.info,  Node: Local Reg Vars,  Prev: Global Reg Vars,  Up: Explicit Reg Vars
  848. Specifying Registers for Local Variables
  849. ----------------------------------------
  850.    You can define a local register variable with a specified register
  851. like this:
  852.      register int *foo asm ("a5");
  853. Here `a5' is the name of the register which should be used.  Note that
  854. this is the same syntax used for defining global register variables,
  855. but for a local variable it would appear within a function.
  856.    Naturally the register name is cpu-dependent, but this is not a
  857. problem, since specific registers are most often useful with explicit
  858. assembler instructions (*note Extended Asm::.).  Both of these things
  859. generally require that you conditionalize your program according to cpu
  860. type.
  861.    In addition, operating systems on one type of cpu may differ in how
  862. they name the registers; then you would need additional conditionals.
  863. For example, some 68000 operating systems call this register `%a5'.
  864.    Eventually there may be a way of asking the compiler to choose a
  865. register automatically, but first we need to figure out how it should
  866. choose and how to enable you to guide the choice.  No solution is
  867. evident.
  868.    Defining such a register variable does not reserve the register; it
  869. remains available for other uses in places where flow control determines
  870. the variable's value is not live.  However, these registers are made
  871. unavailable for use in the reload pass.  I would not be surprised if
  872. excessive use of this feature leaves the compiler too few available
  873. registers to compile certain functions.
  874. File: gcc.info,  Node: Alternate Keywords,  Next: Incomplete Enums,  Prev: Explicit Reg Vars,  Up: C Extensions
  875. Alternate Keywords
  876. ==================
  877.    The option `-traditional' disables certain keywords; `-ansi'
  878. disables certain others.  This causes trouble when you want to use GNU C
  879. extensions, or ANSI C features, in a general-purpose header file that
  880. should be usable by all programs, including ANSI C programs and
  881. traditional ones.  The keywords `asm', `typeof' and `inline' cannot be
  882. used since they won't work in a program compiled with `-ansi', while
  883. the keywords `const', `volatile', `signed', `typeof' and `inline' won't
  884. work in a program compiled with `-traditional'.
  885.    The way to solve these problems is to put `__' at the beginning and
  886. end of each problematical keyword.  For example, use `__asm__' instead
  887. of `asm', `__const__' instead of `const', and `__inline__' instead of
  888. `inline'.
  889.    Other C compilers won't accept these alternative keywords; if you
  890. want to compile with another compiler, you can define the alternate
  891. keywords as macros to replace them with the customary keywords.  It
  892. looks like this:
  893.      #ifndef __GNUC__
  894.      #define __asm__ asm
  895.      #endif
  896.    `-pedantic' causes warnings for many GNU C extensions.  You can
  897. prevent such warnings within one expression by writing `__extension__'
  898. before the expression.  `__extension__' has no effect aside from this.
  899. File: gcc.info,  Node: Incomplete Enums,  Next: Function Names,  Prev: Alternate Keywords,  Up: C Extensions
  900. Incomplete `enum' Types
  901. =======================
  902.    You can define an `enum' tag without specifying its possible values.
  903. This results in an incomplete type, much like what you get if you write
  904. `struct foo' without describing the elements.  A later declaration
  905. which does specify the possible values completes the type.
  906.    You can't allocate variables or storage using the type while it is
  907. incomplete.  However, you can work with pointers to that type.
  908.    This extension may not be very useful, but it makes the handling of
  909. `enum' more consistent with the way `struct' and `union' are handled.
  910. File: gcc.info,  Node: Function Names,  Prev: Incomplete Enums,  Up: C Extensions
  911. Function Names as Strings
  912. =========================
  913.    GNU CC predefines two string variables to be the name of the current
  914. function.  The variable `__FUNCTION__' is the name of the function as
  915. it appears in the source.  The variable `__PRETTY_FUNCTION__' is the
  916. name of the function pretty printed in a language specific fashion.
  917.    These names are always the same in a C function, but in a C++
  918. function they may be different.  For example, this program:
  919.      extern "C" {
  920.      extern int printf (char *, ...);
  921.      }
  922.      
  923.      class a {
  924.       public:
  925.        sub (int i)
  926.          {
  927.            printf ("__FUNCTION__ = %s\n", __FUNCTION__);
  928.            printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
  929.          }
  930.      };
  931.      
  932.      int
  933.      main (void)
  934.      {
  935.        a ax;
  936.        ax.sub (0);
  937.        return 0;
  938.      }
  939. gives this output:
  940.      __FUNCTION__ = sub
  941.      __PRETTY_FUNCTION__ = int  a::sub (int)
  942.